﻿using EV.EventBus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ZoneLine.ViewModels;

namespace ZoneLine.Views
{
    /// <summary>
    /// HistoryData.xaml 的交互逻辑
    /// </summary>
    public partial class HistoryData : Window
    {
        public HistoryData()
        {
            InitializeComponent();
            MessageCenter.Subscribe(nameof(HistoryViewModel), Guid.NewGuid().ToString(), (s) =>
            {
                if (s.Source.Payload is string ss)
                {
                    this.Dispatcher.Invoke(() =>
                    {
                        var viewModel = this.DataContext as HistoryViewModel;
                        if (viewModel != null)
                        {
                            viewModel.Code = ss;
                        }
                    });
                }
                return Task.CompletedTask;
            });
            this.Loaded += HistoryData_Loaded;
            this.Closed += HistoryData_Closed;
        }

        private void HistoryData_Closed(object sender, EventArgs e)
        {
            MessageCenter.Unsubscribe("openinfoview", nameof(HistoryData));
        }

        private void HistoryData_Loaded(object sender, RoutedEventArgs e)
        {
            MessageCenter.Unsubscribe("openinfoviewAbs", nameof(HistoryData));
            var viewBase = this;
            MessageCenter.Subscribe("openinfoviewAbs", nameof(HistoryData), (s) =>
            {
                Dispatcher.Invoke(() =>
                {
                    if (s.Source.Payload is string ss)
                    {
                        var view = StationResultInfoView.GetView(ss, true);
                        view.Owner = viewBase;
                        view.Show();
                    }
                });
                return Task.CompletedTask;
            });

            MessageCenter.Subscribe("openinfoview", nameof(HistoryData), (s) =>
            {
                Dispatcher.Invoke(() =>
                {
                    if (s.Source.Payload is string ss)
                    {
                        var view = StationResultInfoView.GetView(ss, false);
                        view.Owner = viewBase;
                        view.Show();
                    }
                });
                return Task.CompletedTask;
            });
        }
    }
}
